package com.jsb.laundry.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jsb.laundry.common.exception.*;
import com.jsb.laundry.model.dto.posts.PostFavourAddRequest;
import com.jsb.laundry.model.dto.posts.PostFavourQueryRequest;
import com.jsb.laundry.model.dto.posts.PostQueryRequest;
import com.jsb.laundry.model.entity.JsbPosts;
import com.jsb.laundry.model.vo.PostsVO;
import com.jsb.laundry.service.JsbPostFavoritesService;
import com.jsb.laundry.service.JsbPostsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 帖子收藏接口
 */
@RestController
@RequestMapping("/posts/favour")
@Api(value = "帖子相关接口",tags = "帖子")
@Slf4j
public class PostFavourController {

    @Resource
    private JsbPostFavoritesService postFavourService;

    @Resource
    private JsbPostsService postService;

    /**
     * 收藏 / 取消收藏
     *
     * @param postFavourAddRequest
     * @param request
     * @return resultNum 收藏变化数
     */
    @PostMapping("/op")
    @ApiOperation("帖子 收藏 / 取消收藏")
    public BaseResponse<Integer> doPostFavour(@RequestBody PostFavourAddRequest postFavourAddRequest,
                                              HttpServletRequest request) {
        if (postFavourAddRequest == null || postFavourAddRequest.getPostId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String userId = postFavourAddRequest.getUserId();
        long postId = postFavourAddRequest.getPostId();
        int result = postFavourService.doPostFavour(postId, userId);
        return ResultUtils.success(result);
    }

//    /**
//     * 获取我收藏的帖子列表
//     *
//     * @param postQueryRequest
//     * @param request
//     */
//    @PostMapping("/my/favour/list")
//    public BaseResponse<Page<PostsVO>> listMyFavourPostByPage(@RequestBody PostQueryRequest postQueryRequest,
//            HttpServletRequest request) {
//        if (postQueryRequest == null) {
//            throw new BusinessException(ErrorCode.PARAMS_ERROR);
//        }
//        String userId = postQueryRequest.getUserId();
//        long current = postQueryRequest.getPageNum();
//        long size = postQueryRequest.getPageSize();
//        // 限制爬虫
//        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
//        Page<JsbPosts> postPage = postFavourService.listFavourPostByPage(new Page<>(current, size),
//                postService.getQueryWrapper(postQueryRequest), userId);
//        return ResultUtils.success(postService.getPostVOPage(postPage, request));
//    }

    /**
     * 获取用户收藏的帖子列表
     *
     * @param postFavourQueryRequest
     * @param request
     */
    @PostMapping("/user/list")
    @ApiOperation("获取用户收藏的帖子列表")
    public BaseResponse<Page<PostsVO>> listFavourPostByPage(@RequestBody PostFavourQueryRequest postFavourQueryRequest,
                                                            HttpServletRequest request) {
        if (postFavourQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = postFavourQueryRequest.getPageNum();
        long size = postFavourQueryRequest.getPageSize();
        String userId = postFavourQueryRequest.getUserId();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20 || userId == null, ErrorCode.PARAMS_ERROR);
        Page<JsbPosts> postPage = postFavourService.listFavourPostByPage(new Page<>(current, size),
                postService.getQueryWrapper(postFavourQueryRequest.getPostQueryRequest()), userId);
        return ResultUtils.success(postService.getPostVOPage(postFavourQueryRequest.getPostQueryRequest().getLoginUserId(), postPage, request));
    }
}
